<template>
  <div class="bottom">
    <p>总购买数量: {{ totalNum }}</p>
    <p>
      <button class="pay">去结算</button>
      <button class="clear" @click="clearCart">清空购物车</button>
      <button class="clear_select" @click="delAllSelect">删除所有已选中</button>
      <button class="list">返回列表页</button>
    </p>
    <p>总价格 : ￥ <span>{{ totalPrice }}</span></p>
  </div>
</template>

<script>

export default {
  name: 'CartFooter',
  props: ['list'],
  methods: {
    clearCart() {
      // this.list = [];
      //不要直接操作prop过来的数据
      //而是通知数据的源头 app 我要清空你的数据
      this.$emit("clearCart");
    },
    delAllSelect() {
      //和上面类似
      this.$emit("delAllSelect");
    },
  },
  computed: {
    //不管是总数量还是总价格 要计算的都是要选中的
    //select为true
    //总数量
    totalNum() {
      return this.list.filter((item) => item.select)
        .reduce((item, prev) => (item += prev.buy_num), 0)
    },
    //总价格
    totalPrice() {
      return this.list.filter((item) => item.select)
        .reduce((item, prev) => (item += prev.buy_num * prev.price), 0)
    },
  }
}

</script>

<style>
.bottom {
  height: 40px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: pink;
}

.bottom>p {
  font-size: 20px;
}

.bottom>p>button {
  font-size: 20px;
  padding: 3px 10px;
  cursor: pointer;
  color: #fff;
}

.bottom>p>button.pay {
  background-color: green;
}

.bottom>p>button.clear {
  background-color: red;
}

.bottom>p>button.clear_select {
  background-color: orange;
}

.bottom>p>button.list {
  background-color: skyblue;
}

.bottom>p>span {
  color: red;
}
</style>
